package com.example.administrator.fangbook.read.bean;

import android.os.Parcel;
import android.os.Parcelable;


import com.example.administrator.fangbook.appliction.HtlApplication;
import com.example.administrator.fangbook.read.library.constants.GlobalConstants;
import com.example.administrator.fangbook.utils.FileUtils;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import org.geometerplus.zlibrary.core.filesystem.ZLFile;

import java.io.File;
import java.io.Serializable;

/**
 * Expose:
 * serialize 为 false 代表不进行序列化
 * deserialize 为false 代表不进行反序列化
 * <p>
 * 由于序列化时 排除某些 属性
 * <p>
 * 需要建立在 GsonBuilder下才会起作用
 * 注意 在 未使用@Expose在反序列化时 会被忽视
 * 具有一定的缺陷性
 * <p>
 * 付费章节选择对应的Chapter实体
 */
public class Chapter implements Serializable, Parcelable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    @Expose
    private Long id;

    /**
     * name
     */
    @Expose
    @SerializedName("n")
    private String name;
    /**
     * 目录名称
     */
    private String vn;//目录名称
    /**
     * 显示权限 1为显示
     */
    @Expose
    @SerializedName("t")
    private int isRead;
    /**
     * 位置
     */
    private Long vd;
    private int isAdd;
    /**
     * 书籍id
     */
    private String bookId;
    /**
     * 章节指针
     */
    private String chapterindex; // 章节index
    /**
     * 是否免费
     */
    private boolean isFree = false;
    /**
     * 卷id
     */
    private String volumeId = "";
    /**
     * 排序
     */
    private String desc = "";
    /**
     * 是否购买
     */
    private String isbuy;
    /**
     * 创建数据
     */
    private String create_date;
    /**
     * 是新闻？
     */
    private String isnews;

    /**
     * 是否vip
     */
    @Expose
    @SerializedName("vip")
    private int isVIP;

    /**
     * 使用ZLFile构建章节
     *
     * @param file
     * @return
     */
    public static Chapter buildChapterByZLFile(ZLFile file) {
        final Chapter chapter = new Chapter();
        chapter.setBookId(file.getBookId());
        chapter.setName(file.getChapterName());
        chapter.setId(file.getChapterId());
        return chapter;
    }


    /**
     * 得到保存没有加密文件的路径
     *
     * @param bookId
     * @param chapterId
     * @return
     */
    public static String getSaveFilePlainTextPath(String bookId, String chapterId) {
        String path = GlobalConstants.BOOK_PATH + "/" + bookId + "/" + chapterId;
        return path;
    }

    /**
     * 得到本地文件的路径
     *
     * @param bookId
     * @param chapterId
     * @return
     */
    public static String getLocalTxtChapterFilePath(String bookId, String chapterId) {
        String path = GlobalConstants.LOCAL_BOOK_PATH + "/" + bookId + "/" + chapterId;
        return path;
    }

    /**
     * 得到保存文件路径V2
     *
     * @param bookId
     * @param volumeId
     * @param chapterId
     * @return
     */
    public static String getSaveFilePathV2(String bookId, String volumeId, String chapterId) {
        String path = GlobalConstants.BOOK_PATH + "/" + bookId + "/" + volumeId + "/" + chapterId + ".k1";
        return path;
    }


    /**
     * 得到内置书路径
     *
     * @param bookId
     * @param chapterId
     * @return
     */
    public static String getBuiltInPath(String bookId, String chapterId) {
        return GlobalConstants.ASSET_BOOK_PATH + "/" + bookId + "/" + chapterId;
    }

    /**
     * 判断当前章节是否下载
     *
     * @param bookId
     * @param chapterId
     * @return
     */
    public synchronized static boolean hasDownload(String bookId, String chapterId) {
        //本地是否存在
        if (isInBookPath(bookId, chapterId)) {
            return true;
        }
        //资源文件里面是否包含
        return HtlApplication.getApplication().isBookChapterInAsset(bookId, chapterId);
    }

    /**
     * 判断没有明文文件是否存在
     *
     * @param bookId
     * @param chapterId
     * @return
     */
    public synchronized static boolean isInBookPath(String bookId, String chapterId) {
        String path = getSaveFilePlainTextPath(bookId, chapterId);
        return FileUtils.isFileExist(path);
    }

    /**
     * 保存章节内容
     *
     * @param v4 从服务器获取的章节内容
     * @return 是否保存成功
     */
    public synchronized static boolean saveChapterContent(ChapterContentV4 v4) {
        if (v4 != null && v4.getContents() != null) {
            File f = FileUtils.saveChapterContent(v4.getBookId(), v4.getChapterId(), v4.getContents());
            if (f != null && f.exists()) {
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (o != null && o instanceof Chapter) {
            Chapter c = (Chapter) o;
            if (null != c.id) {
                return c.id.equals(id);
            }
        }
        return false;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(id);
        dest.writeString(name);
        dest.writeInt(isRead);


    }

    public void readFromParcel(Parcel in) {
        id = in.readLong();
        name = in.readString();
        isRead = in.readInt();
    }

    public static final Creator<Chapter> CREATOR = new Creator<Chapter>() {
        @Override
        public Chapter createFromParcel(Parcel source) {
            Chapter chapter = new Chapter();
            chapter.readFromParcel(source);
            return chapter;
        }

        @Override
        public Chapter[] newArray(int size) {
            return new Chapter[size];
        }
    };


    public String getId() {
        return id + "";
    }

    public void setId(String id) {
        try {
            this.id = Long.valueOf(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVn() {
        return vn;
    }

    public void setVn(String vn) {
        this.vn = vn;
    }

    public Long getVd() {
        return vd;
    }

    public void setVd(Long vd) {
        this.vd = vd;
    }

    public int isRead() {
        return isRead;
    }

    public void setIsRead(int isRead) {
        this.isRead = isRead;
    }

    public int getIsAdd() {
        return isAdd;
    }

    public void setIsAdd(int isAdd) {
        this.isAdd = isAdd;
    }


    public String getBookId() {
        return bookId;
    }

    public void setBookId(String bookId) {
        this.bookId = bookId;
    }


    public int isVIP() {
        return isVIP;
    }

    public void setVIP(int isVIP) {
        this.isVIP = isVIP;
    }

    public String getChapterindex() {
        return chapterindex;
    }

    public void setChapterindex(String chapterindex) {
        this.chapterindex = chapterindex;
    }

    public boolean isFree() {
        return isFree;
    }

    public void setIsFree(boolean isFree) {
        this.isFree = isFree;
    }

    public String getVolumeId() {
        return volumeId;
    }

    public void setVolumeId(String volumeId) {
        this.volumeId = volumeId;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getIsbuy() {
        return isbuy;
    }

    public void setIsbuy(String isbuy) {
        this.isbuy = isbuy;
    }

    public String getCreate_date() {
        return create_date;
    }

    public void setCreate_date(String create_date) {
        this.create_date = create_date;
    }

    public String getIsnews() {
        return isnews;
    }

    public void setIsnews(String isnews) {
        this.isnews = isnews;
    }

    @Override
    public String toString() {
        return "Chapter{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", vn='" + vn + '\'' +
                ", isRead=" + isRead +
                ", vd=" + vd +
                ", isAdd=" + isAdd +
                ", bookId='" + bookId + '\'' +
                ", chapterindex='" + chapterindex + '\'' +
                ", isFree=" + isFree +
                ", volumeId='" + volumeId + '\'' +
                ", desc='" + desc + '\'' +
                ", isbuy='" + isbuy + '\'' +
                ", create_date='" + create_date + '\'' +
                ", isnews='" + isnews + '\'' +
                ", isVIP=" + isVIP +
                '}';
    }
}
